package com.hlg.xboot.service.sys;

import com.digi_zones.common.utils.DateUtils;
import com.hlg.xboot.dao.sys.SysUserRepository;
import com.hlg.xboot.enums.Gender;
import com.hlg.xboot.exceptions.ParameterException;
import com.hlg.xboot.exceptions.ServerBizException;
import com.hlg.xboot.model.sys.SysUser;
import com.hlg.xboot.service.BaseService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: 中视数讯</p>
 * <p>Site: www.digi-zones.com</p>
 *
 * @author gaol
 * @version 1.0
 * @date 2017-07-24 11:26:36
 */
@Service
public class SysUserService extends BaseService<SysUser, SysUserRepository> {

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return 用户信息
     * @throws Exception
     */
    public SysUser login(String username, String password) throws Exception {
        return repository.findByUsernameAndPassword(username, password);
    }

    /**
     * 添加用户
     * @param user      用户信息
     * @return
     * @throws ServerBizException
     */
    public SysUser add(SysUser user) throws ServerBizException {
        if (repository.findByUsername(user.getUsername()) != null) {
            throw new ServerBizException("用户[" + user.getUsername() + "]已存在");
        }
        user.setPassword(DigestUtils.md5Hex(user.getPassword()));
        return repository.save(user);
    }

    /**
     * 修改密码
     * @param username      用户名
     * @param password      密码
     * @param newPassword   新密码
     * @throws Exception
     */
    public void updatePassword(String username, String password, String newPassword) throws Exception {
        SysUser sysUser = repository.findByUsernameAndPassword(username, DigestUtils.md5Hex(password));
        if (sysUser == null) {
            throw new ParameterException("旧密码不正确");
        }
        sysUser.setPassword(DigestUtils.md5Hex(newPassword));
        super.update(sysUser);
    }

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return 用户对象
     */
    public SysUser findByUsername(String username){
        return repository.findByUsername(username);
    }

    @Override
    public List<String> getExportHeader() {
        return Arrays.asList("用户名","真实姓名", "邮箱", "性别", "手机", "最后登录时间", "最后登录IP", "创建时间");
    }

    @Override
    public List<List<String>> getExportRows(SysUser sysUser, Pageable pageable) throws Exception {
        List<List<String>> rows = new ArrayList<>();
        List<SysUser> sysUsers = find(sysUser, pageable).getContent();
        sysUsers.forEach((user) -> {
            rows.add(Arrays.asList(user.getUsername(),
                    user.getRealName(),
                    user.getEmail(),
                    Gender.getGender(user.getSex()).getDesc(),
                    user.getPhone(),
                    DateUtils.format(user.getLastLoginTime()),
                    user.getLastLoginIP(),
                    DateUtils.format(user.getCreateTime())));
        });
        return rows;
    }

    @Override
    public String getExportFileName() {
        return "系统用户";
    }

}


